using System;
using System.Data;
using System.Web.UI;
using DayPilot.Web.Ui.Design;

public partial class Calendar_New : Page
{
    private DataTable table;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBoxStart.Text = Convert.ToDateTime(Request.QueryString["start"]).ToString();
            TextBoxEnd.Text = Convert.ToDateTime(Request.QueryString["end"]).ToString();

            TextBoxName.Focus();
        }
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        DentistCalendar cev = new DentistCalendar();
        cev.eventstart = Convert.ToDateTime(TextBoxStart.Text);
        cev.eventend = Convert.ToDateTime(TextBoxEnd.Text);
        cev.name = TextBoxName.Text;
        var dtime = cev.eventend.Value.TimeOfDay;
        int h = Convert.ToInt32(dtime.TotalHours);
        int th = 24;
        if (Convert.ToDateTime(TextBoxEnd.Text).Date > Convert.ToDateTime(TextBoxStart.Text).Date)
        {
            lblMessage.Text = "You have entered a wrong date";
            lblMessage.Visible = true;
            // ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:alert(You have entered a wrong date);", true);
        }
        else if (h > th)
        {
            lblMessage.Text = "You have entered a wrong Time";
            lblMessage.Visible = true;
           
            
        }
        else
        {
            if (Request.QueryString["dentistId"] != null)
            {
                Session["dentistId"] = Request.QueryString["dentistId"].ToString();
                //FillOpenViewLabels();
            }
            cev.dentistId = Convert.ToInt32(Session["dentistId"]);

            AddEventd(cev);
            Modal.Close(this, "OK");
        }
    }

    private string dbInsertEvent(DateTime start, DateTime end, string name, string resource)
    {
        initData();

        string id = Guid.NewGuid().ToString();

        #region Simulation of database update

        DataRow dr = table.NewRow();
        dr["start"] = start;
        dr["end"] = end;
        dr["id"] = id;
        dr["name"] = name;
        dr["column"] = resource;

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        return id;
    }

    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Modal.Close(this);
    }

    private void initData()
    {
        if (Session["AllFeatures"] == null)
        {
            Session["AllFeatures"] = DataGenerator.GetData();
        }
        table = (DataTable)Session["AllFeatures"];
    }
    public bool AddEventd(DentistCalendar modl)
    {
        DataCalendarDataContext DC = new DataCalendarDataContext();
        try
        {


            DC.DentistCalendars.InsertOnSubmit(modl);

            DC.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

}
